/***************************************************************************
 * Copyright (C) 2005 Global Biodiversity Information Facility Secretariat.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 ***************************************************************************/
package org.gbif.agi.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.appfuse.model.BaseObject;

/**
 * @author trobertson
 */
@Entity
@Table(name="genus")
public class Genus extends BaseObject implements Comparable<Object> {
	private static final long serialVersionUID = -8374643139537692973L;
	
	private Long id;
	private String fullScientificName;
	// the primary canonical atom
	private String canonical;
	// the authors + years + publication
	private String authorship;
	// the year if it can be pulled out 
	private String year;
	
	private List<GenusUsage> genusUsages;
	// to means this side is the relationship TO
	private List<GenusRelationship> toRelationships;
	private List<GenusRelationship> fromRelationships;
	
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(columnDefinition="int")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(name="scientific_name", nullable=false)
	public String getFullScientificName() {
		return fullScientificName;
	}
	public void setFullScientificName(String fullScientificName) {
		this.fullScientificName = fullScientificName;
	}
	
	@Column(name="canonical", nullable=false)
	public String getCanonical() {
		return canonical;
	}
	public void setCanonical(String canonical) {
		this.canonical = canonical;
	}
	
	public String getAuthorship() {
		return authorship;
	}
	public void setAuthorship(String authorship) {
		this.authorship = authorship;
	}
	
	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		Genus myClass = (Genus) object;
		return new CompareToBuilder().append(this.authorship,
				myClass.authorship).append(this.fullScientificName,
				myClass.fullScientificName).append(this.canonical, myClass.canonical)
				.append(this.id, myClass.id).toComparison();
	}
	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof Genus)) {
			return false;
		}
		Genus rhs = (Genus) object;
		return new EqualsBuilder().append(
				this.authorship, rhs.authorship).append(
				this.fullScientificName, rhs.fullScientificName).append(
				this.canonical, rhs.canonical).append(this.id, rhs.id).isEquals();
	}
	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(1904495293, 629231877).append(this.authorship).append(
				this.fullScientificName).append(this.canonical).append(this.id)
				.toHashCode();
	}
	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("id", this.id)
			.append("fullScientificName",this.fullScientificName)
			.append("canonical", this.canonical)
			.append("authorship", this.authorship)
			.toString();
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	
	@OneToMany
	@JoinColumn (name="to_genus_id",updatable=false, insertable=false)
	public List<GenusRelationship> getToRelationships() {
		return toRelationships;
	}
	public void setToRelationships(List<GenusRelationship> toRelationships) {
		this.toRelationships = toRelationships;
	}
	@OneToMany
	@JoinColumn (name="from_genus_id",updatable=false, insertable=false)
	public List<GenusRelationship> getFromRelationships() {
		return fromRelationships;
	}
	public void setFromRelationships(List<GenusRelationship> fromRelationships) {
		this.fromRelationships = fromRelationships;
	}
	@OneToMany
	@JoinColumn (name="genus_id",updatable=false, insertable=false)
	public List<GenusUsage> getGenusUsages() {
		return genusUsages;
	}
	public void setGenusUsages(List<GenusUsage> genusUsages) {
		this.genusUsages = genusUsages;
	}
}
